package com.whales.lms.rms.web.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import anyframe.common.Page;

import com.whales.lms.rms.web.model.HardwareVO;
import com.whales.shared.exception.BaseException;
import com.whales.shared.service.GenericService;

@Service
public class HardwareService extends GenericService<HardwareVO, HardwareVO> {
	@Inject
	private ManualService manualService;
	@Inject
	private ScheduleService scheduleService;
	@Inject
	private PcItemService pcItemService;
	
	@Override
	public int create(HardwareVO hardwareVO) throws Exception{
		HardwareVO checkVO = new HardwareVO();
		checkVO.setName(hardwareVO.getName());
		if(this.findByName(checkVO).size() > 0){
			throw new BaseException(super.getMessage("rms.msg.exitsDuplicatedHardware"));
		}
		return super.create("hardware.createHardware", hardwareVO);
	}
	
	@Override
	public int delete(HardwareVO hardwareVO) throws Exception{
		for (int i = 0; i < hardwareVO.getIds().length; i++) {
			HardwareVO vo = new HardwareVO();
			vo.setId(hardwareVO.getIds()[i]);
			manualService.deleteByParentId(vo);
			super.delete("hardware.deleteHardwareById", vo);
			scheduleService.deleteZonePcByPcId(vo);
			pcItemService.deleteByHwSwId(vo);
		}
		return super.delete(hardwareVO);
	}
	
	@Override
	public HardwareVO findInfo(HardwareVO hardwareVO) throws Exception{
		return super.findInfo("hardware.getHardwareById", hardwareVO);
	}
	
	public List<HardwareVO> findByName(HardwareVO hardwareVO) throws Exception{
		return super.getList("hardware.findByName", hardwareVO);
	}

	@Override
	public List<HardwareVO> getList(HardwareVO hardwareVO) throws Exception{
		return super.getList("hardware.getHardwareList", hardwareVO);
	}
	
	@Override
	public Page getPageList(HardwareVO vo) throws Exception {
		return super.getPageList("hardware.getHardwareList", vo, vo.getStart(), vo.getLimit());
	}
	
	@SuppressWarnings("unchecked")
	public List<HardwareVO> getHardwareTypes() throws Exception{
		return commonDao.findList("hardware.getHardwareTypes", "");
	}
	
	@Override
	public int update(HardwareVO hardwareVO) throws Exception{
		return super.update("hardware.updateHardwareById", hardwareVO);
	}
}
